/*
gobanjs - html5 SGF viewer for the Game of Go 

Copyright (C) 2013  Daniel Cioata
Visit http://www.govortex.ro/ for documentation, updates and examples.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/    

"use strict";

function View(controller,index)
{
    this.controller = controller;
    this.index = index;   
    this.stonesize = 0;
    
    this.main_div        = null;
    this.gameinfo_div    = null;
    this.goban_container = null;
    this.goban_canvas    = null;    
    this.comment_div     = null;
    this.stage           = null;
    this.variations      = null;
    
}

View.prototype.init=function(parent)
{
    var cfg;
    
    this.createDiagram(parent);        
    cfg=this.updateConfig();
    this.createVariations(cfg.stonesize);        
    this.displayGameInfo();
    this.stage = new SGoStage(cfg);
    this.stage.init();                    
}

View.prototype.updateConfig=function()
{
    var w,h,cfg,ssize,bsize,bounds;
    
    bsize  = this.controller.model.getBoardSize();               
    bounds = this.controller.model.getBounds();

    w = bounds.right-bounds.left;
    h = bounds.bottom-bounds.top;

    if(w>h)
        ssize=this.controller.config.maxwidth/(w+1);
    else
        ssize=this.controller.config.maxwidth/(h+1);   
        
    this.resize((w+1)*ssize,(h+1)*ssize);    
            
    cfg={ 
            "view":this,
            "gridsize":{"width":w,"height":h},
            "boardsize":bsize,
            "bounds":bounds,
            "stonesize":ssize            
           };    
    return cfg;
}

View.prototype.resize=function(w,h)
{
    this.goban_canvas.width=w;
    this.goban_canvas.height=h;
}

View.prototype.createElem=function(obj)
{    
    var el;
    el=document.createElement(obj.type);  
    el.className=obj.cname;
    el.id=obj.cname+"-"+obj.index;
    if(obj.parent!=null)
        obj.parent.appendChild(el);  
    return el;  
}

View.prototype.createDiagram=function(parent)
{
    
    var i,obj,butlist,snav,li,a,index;
    
    index=this.index;
    
    butlist=["start","prev","next","end"];
    
    obj={"parent":null, "type":"div", "cname":"sgo_main", "index":index};
    this.main_div=this.createElem(obj);
    
    /* sgo_gameinfo */
    obj={"parent":this.main_div, "type":"div", "cname":"sgo_gameinfo", "index":index};
    this.gameinfo_div=this.createElem(obj);
    
    /* goban canvas container */
    obj={"parent":this.main_div, "type":"div", "cname":"sgo_canvas_container", "index":index};
    this.goban_container=this .createElem(obj);
    
    /* canvas */    
    obj={"parent":this.goban_container, "type":"canvas", "cname":"sgo_canvas", "index":index};
    this.goban_canvas=this.createElem(obj);
    this.goban_canvas.width  = 480;
    this.goban_canvas.height = 480;
    
    /*comments*/
    obj={"parent":this.main_div, "type":"div", "cname":"sgo_comment", "index":index};
    this.comment_div=this.createElem(obj);    
    
    /* snav */        
    obj={"parent":this.main_div, "type":"ul", "cname":"sgo_nav", "index":index};
    snav=this.createElem(obj);    
    
    /* snav buttons */
    for(i=0;i<butlist.length;i++)
    {        
        li=document.createElement("li");
        a=document.createElement("a");
        a.id=butlist[i]+"-"+index;
        a.className="sgo_button btn_orange";
        a.innerHTML=butlist[i];
        a.addEventListener('click',handleEvent,false);
        a.href="#";
        li.appendChild(a);      
        snav.appendChild(li);  
    }
    this.main_div.appendChild(snav);
    parent.appendChild(this.main_div);
}

View.prototype.getSize=function () 
{
    var w = 0, h = 0;
    if( typeof( window.innerWidth ) == 'number' ) 
    {
        //Non-IE
        w = window.innerWidth;
        h = window.innerHeight;
    } 
    else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
    {
        //IE 6+ in 'standards compliant mode'
        w = document.documentElement.clientWidth;
        h = document.documentElement.clientHeight;
    } 
    else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
    {
        //IE 4 compatible
        w = document.body.clientWidth;
        h = document.body.clientHeight;
    }
    window.alert( 'Width = ' + w );
    window.alert( 'Height = ' + h );
}

View.prototype.displayGameInfo=function(sz)
{      
    var gi,gistr;
    if(this.controller.config.filetype=="problem")
        this.gameinfo_div.innerHTML = this.controller.model.getNextToPlay();
    else
    {
        gi=this.controller.model.getGameInfo();
        gistr  ="<div><span style=\"color:white\">"+gi.PW+"</span> vs. ";
        gistr +="<span style=\"color:black\">"+gi.PB+"</span></div>";
        gistr+="<div>"+gi.EV+"</div>";
        this.gameinfo_div.innerHTML = gistr;
    }
        
}

View.prototype.createVariations=function(sz)
{
    var nrvar,i,el;
    
    if(this.variations == null)
    {
        nrvar=this.controller.config.maxvar;
        this.variations=new Array(nrvar);
        for(i=0;i<nrvar;i++)
        {
            el=document.createElement('div');
            el.id="sgo_var-"+this.index+"-"+i;
            el.className = "sgo_var";
            el.style.position="absolute";
            el.style.width  = sz+"px";
            el.style.height = sz+"px";
            el.style.lineHeight=sz+"px";
            el.addEventListener('click',handleEvent,false);
            this.variations[i]=el;
            this.goban_container.appendChild(el);                        
        }
    }
}

View.prototype.displayVariation=function(index,x,y,label)
{
    
    if(index >= this.variations.length)
    {
        console.warn("Variation index out of range");
        return;        
    }  
    this.variations[index].style.left = x + "px";
    this.variations[index].style.top = y + "px";
    this.variations[index].style.display = "inline";
    this.variations[index].innerHTML = label;
}

View.prototype.showVariations=function()
{
    var node, stone, cld, bounds, cnr, i, x, y;
       
    bounds=this.controller.model.getBounds();
    /* display next move */
    if(this.controller.config.shownext==true)
    {
        node=this.controller.model.getNextNode();
        if(node!=null)
            stone = node.getStone();
        if(stone!=null)
        {
            x=this.stage.getX(stone.x - bounds.left);
            y=this.stage.getY(stone.y - bounds.top);        
            this.displayVariation(0, x , y, "1");            
        }
    }
    /* display variations */
    if(this.controller.config.showvars==true)
    {
        node=this.controller.model.getCurrent();            
        if(node!=null)
        {
            cnr=node.getNumberOfChildren();
            for(i=0;i<cnr;i++)
            {
                cld=node.getChild(i);
                stone=cld.getStone();
                if(stone!=null)
                {
                    x = this.stage.getX(stone.x - bounds.left);
                    y = this.stage.getY(stone.y - bounds.top);
                    this.displayVariation(i+1, x , y, " "+(i+2));                    
                }
            }
        }
    }    

}

View.prototype.hideVariations=function()
{
    var i;
    for (i=0;i<this.variations.length;i++)
        if(this.variations[i].style.display != "none")
            this.variations[i].style.display = "none";

}

View.prototype.update=function()
{
    var i,j,x,y,st,st1,goban,bounds,node;
    
    goban=this.controller.model.board;    
    bounds=this.controller.model.getBounds();
    
    this.stage.drawGrid();
    /* console.log(this.update_ascii()); */
    
    for(j=bounds.top;j<bounds.bottom;j++)
        for(i=bounds.left;i<bounds.right;i++)
        {
            st=goban.stones[i][j];
            
            if(st!=null)
            {                
                x=st.x-bounds.left;
                y=st.y-bounds.top;                                        
                this.stage.drawStone(st.color,x,y);
            }
            else
            {
                if(this.controller.model.isHandi(i,j))
                {
                    x=i-bounds.left;
                    y=j-bounds.top;      
                    this.stage.drawHandi(x,y);
                }
            }
        }  
    /* draw stone index */
    node=this.controller.model.getCurrent(); 
    if(node!=null)
        st = node.getStone();
    if(st!=null)
    {
        x=st.x-bounds.left;
        y=st.y-bounds.top; 
        if(st.color==SGFProp.ID_B)        
            this.stage.drawStoneIndex(st.index,'#FFF',x,y);
        else if(st.color==SGFProp.ID_W)
            this.stage.drawStoneIndex(st.index,'#000',x,y);
    }
    this.hideVariations();
    this.showVariations();        
}

View.prototype.update_ascii=function()
{
    var i, j, goban, bounds, prop, str, elem;
        
    goban=this.controller.model.board;    
    bounds=this.controller.model.getBounds();
    
    str="";
    for(j=bounds.left;j<bounds.right;j++)
      str+=' '+j%10;
    str+="\n";
   
   for(j=bounds.top;j<bounds.bottom;j++)
   {
      str+=j%10;
      for(i=bounds.left;i<bounds.right;i++)
      {
            prop=goban.stones[i][j];
    	    if(prop!=null)
    	    {    	        
    	        if(prop.color==SGFProp.ID_B)
        	        str+=' '+'#';
        	    else if(prop.color==SGFProp.ID_W)
        	        str+=' '+'o';
    	    }
    	    else
    	    {
    	        if(goban.isHandi(i,j))
    	            str+=' ,';
    	        else
    	           str+=' .';
    	    }
       }
       str+="\n";       
   }      
   return str;
}

